<?php
namespace CLASSES
{
	class User extends \JClass
	{
		protected
			/******************************************************/
			//	원본 테이블 정보
			/******************************************************/
			$user_seq			= 0,
			$user_type			= 'USER',
			$user_group_seq		= 0,
			$user_id			= '',
			$user_passwd		= '',
			$user_name			= '',
			$user_nick			= '',
			$user_birth_date	= '',
			$user_phone			= '',
			$user_mobile		= '',
			$user_email			= '',
			$user_sex			= 'M',
			$user_memo			= '',
			$user_time			= '',
			$user_del_flag		= 'F',
			/******************************************************/
			//	추가 정보
			/******************************************************/
			$user_default_commute_times = array();

		/**
		 * 	생성자
		 */
		function __construct($data=array())
		{
			parent::__construct();
			if(count($data) > 0)	$this->__init($data);
			$ci = &get_instance();
			if(isset($ci->User) == false)	$ci->load->model("User");
		}

		/**
		 *	데이터 초기화 시키기
		 */
		public function __init($data)
		{
			parent::__init($data);
			self::dformat();
		}

		/**
		 *	 ...
		 *	@param string	user_seq
		 */
		public function load($user_seq)
		{
			/*------------------------------------------------------------*/
			$ci = & get_instance();
			/*------------------------------------------------------------*/
			$data = $ci->User->cLoad($user_seq);
			self::__init($data);
		}

		public function loadById($user_id)
		{
			/*------------------------------------------------------------*/
			$ci = & get_instance();
			/*------------------------------------------------------------*/
			$data = $ci->User->cLoadById($user_id);
			self::__init($data);
		}

		/**
		 *	데이터 출력 포맷 세팅 및 기본값 정의
		 */
		public function dformat()
		{
		}

		/**
		 *
		 * @since 2012.07.08
		 * @author kinuz
		 */
		public function autoUpdate($log='')
		{
			if((int)$this->user_seq > 0)
			{
				$this->update($log);
			}
			else
			{
				$this->insert($log);
			}
		}

		/**
		 *
		 */
		public function insert($log='직원 등록')
		{
			/*------------------------------------------------------------*/
			$ci = & get_instance();
			/*------------------------------------------------------------*/
			$this->validate();
			$data = array(	'user_type'			=>	$ci->db->escape_str($this->user_type),
							'user_id'			=>  $ci->db->escape_str($this->user_id),
							'user_passwd'		=>	$ci->db->escape_str($this->user_passwd),
							'user_name'			=>	$ci->db->escape_str($this->user_name),
							'user_group_seq'	=>	$ci->db->escape_str($this->user_group_seq),
							'user_nick'			=>	$ci->db->escape_str($this->user_nick),
							'user_birth_date'	=>	$ci->db->escape_str($this->user_birth_date),
							'user_phone'		=>	$ci->db->escape_str($this->user_phone),
							'user_mobile'		=>	$ci->db->escape_str($this->user_mobile),
							'user_email'		=>	$ci->db->escape_str($this->user_email),
							'user_sex'			=>	$ci->db->escape_str($this->user_sex),
							'user_memo'			=>	$ci->db->escape_str($this->user_memo),
							'user_time'			=>	'UNIX_TIMESTAMP()',
							'user_del_flag'		=>	'F'
						);
			$ci->User->i($data);
			$this->user_seq = $ci->User->insertId();
			/******************************************************/
			parent::setBackupData(array_keys($data));
			$this->backup($log);
			/******************************************************/
		}

		/**
		 *
		 * Enter description here ...
		 * @param string $log
		 */
		public function update($log='사용자 정보 업데이트')
		{
			if($this->user_seq == 0) return false;
			/*------------------------------------------------------------*/
			$ci = & get_instance();
			/*------------------------------------------------------------*/
			$this->validate();
			if(parent::isUpdatable())
			{
				try
				{
					$data = array(	'user_type'			=>	$ci->db->escape_str($this->user_type),
									'user_passwd'		=>	$ci->db->escape_str($this->user_passwd),
									'user_name'			=>	$ci->db->escape_str($this->user_name),
									'user_group_seq'	=>	$ci->db->escape_str($this->user_group_seq),
									'user_nick'			=>	$ci->db->escape_str($this->user_nick),
									'user_birth_date'	=>	$ci->db->escape_str($this->user_birth_date),
									'user_phone'		=>	$ci->db->escape_str($this->user_phone),
									'user_mobile'		=>	$ci->db->escape_str($this->user_mobile),
									'user_email'		=>	$ci->db->escape_str($this->user_email),
									'user_sex'			=>	$ci->db->escape_str($this->user_sex),
									'user_memo'			=>	$ci->db->escape_str($this->user_memo),
									'user_del_flag'		=>	$ci->db->escape_str($this->user_del_flag)
								);
					$ci->User->u($data, array("user_seq = {$ci->db->escape($this->user_seq)}"));
					/******************************************************/
					parent::setBackupData(array_keys($data));
					$this->backup($log);
					/******************************************************/
				}
				catch (\DatabaseAffectedException $e){throw new \DatabaseUpdateException("[$log] ".$e->getMessage());}
			}
		}

	    /**
	     *
	     * Enter description here ...
	     * @param string $id
	     */
	    public function isExistsLoginId($user_id)
	    {
	    	$ci = &get_instance();
	    	return ($ci->User->c("user_id={$ci->User->db->escape($user_id)}") > 0);
	    }

	    /**
	    * 유효성 체크..
	    * @since 2012.07.08
	    * @author kinuz
	    */
	    public function validate()
	    {
	    }

		/**
		 *
		 */
		private function backup($log='')
		{
			/*------------------------------------------------------------*/
			$ci = & get_instance();
			/*------------------------------------------------------------*/
			$ci->User->log($this->user_seq, $log, $this->backup_data);
		}
	}
}